//
//  RegisteredViewController.swift
//  AnJiBei
//
//  Created by 赵鑫 on 15/10/31.
//  Copyright © 2015年 赵鑫. All rights reserved.
//

import UIKit

class RegisteredViewController: BaseViewController {
    
    
    @IBOutlet weak var userNickNameTextField: UITextField!
    
    @IBOutlet weak var userPasswordTextField: UITextField!
    
    @IBOutlet weak var userPhoneTextField: UITextField!
    
    @IBOutlet weak var userVerCodeTextField: UITextField!
    
    
    @IBOutlet weak var getCodeButton: CountDownButton!

    var registerInformation = RegisterModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //self.navigationController

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    /************************基本判断相关操作*********************************/
    //基本检查信息时候合理
    func checkInfomation() {
    
        if userNickNameTextField.text!.isEmpty || userPhoneTextField.text!.isEmpty || userPasswordTextField.text!.isEmpty {
            disPlayMessage("您输入的信息不完整，请输入完整")
        }
        
        else {
        
            if userPasswordTextField.text!.characters.count < 6 {
            
                disPlayMessage("您的密码小于六位，请重新输入")
            }
            else {
            
                if !RegularExpressionsUtils.regularExpressionPhone(userPhoneTextField.text!) {
                
                    disPlayMessage("您输入的手机号不正确，请从新输入")
                }
                
                else {
                
                    if userVerCodeTextField.text!.isEmpty {
                    
                        disPlayMessage("验证码为空，请从新输入")
                    }
                    
                    else {
                        
                        checkVerCode()
                        
                    }
                }
            }
        }
        
    }
    
    //检查验证码是否正确
    func checkVerCode(){
        SMSSDK.commitVerificationCode(userVerCodeTextField.text!, phoneNumber: userPhoneTextField.text!, zone: "86", result: {(error) -> Void in
            if error != nil {
            
                self.disPlayMessage("验证码输入有误，请从新输入")
            }
            else {
                
                self.requestType = 1
                self.requestRegister()
                
            }
        })
    }

    //存储相关信息到本地
    func storeDataLocal() {
    
        UserDefaultUtils.setUserAvatar(registerInformation.userAvatar)
        UserDefaultUtils.setUserPhone(registerInformation.userPhone)
        UserDefaultUtils.setUserSid(registerInformation.userSid)
        UserDefaultUtils.setUserTokenId(registerInformation.tokenId)
    }
    /***************button事件********************/
    //注册
    @IBAction func registerButton(sender: UIButton) {
        
         checkInfomation()
        
    }

    //获取验证码
    @IBAction func getVerCodeButton(sender: UIButton) {
        
        if userPhoneTextField.text!.isEmpty {
        
            disPlayMessage("还没有输入手机号吖")
        }
        
        else {
            getCodeButton.maxSecond = 30
            getCodeButton.countdown = true
            
            //获取验证码
            SMSSDK.getVerificationCodeByMethod(SMSGetCodeMethodSMS, phoneNumber: userPhoneTextField.text!, zone: "86", customIdentifier: nil, result: {(error) -> Void in
                if error != nil {
                
                    debugPrint(error)
                
                } else {
                
                    PrintUntils.printLog("获取", message: "获取验证码成功")
                } })
            
            
        }
        
    }

    /*********************request操作****************************/
    
    func requestRegister() {
    
        let requestReguster = RegisterRequsetModel()
        
        requestReguster.userPhone = userPhoneTextField.text!
        requestReguster.userName = userNickNameTextField.text!
        requestReguster.userPassword = userPasswordTextField.text!
        
        startRequest(Constant.REGIST_NEW_USER_URL, parm: requestReguster)
    }
    

    func requestSuccess(json: AnyObject?, message: String) {
        
        
        switch requestType {
        
        case 1 :
            registerInformation = RegisterModel.jsonToModel(json)
            PrintUntils.printLog("registerInformation", message: registerInformation)
            storeDataLocal()
            self.performSegueWithIdentifier("Register2Main", sender: self)
            
        default:
            break
        }
        


        
    }
    func requestFailure(message: String) {
        //
        PrintUntils.printLog("message", message: message)
        
        disPlayMessage(message)
    }
    func requestTimeOut(message: String) {
        //
        PrintUntils.printLog("message", message: message)
        disPlayMessage(message)


    }
    
    func requestDataNull(message: String) {
        //
        PrintUntils.printLog("message", message: message)
        disPlayMessage(message)


    }
    
    /*************其他操作******************/
    
    //键盘消失
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.userNickNameTextField.resignFirstResponder()
        self.userPasswordTextField.resignFirstResponder()
        self.userPhoneTextField.resignFirstResponder()
        self.userVerCodeTextField.resignFirstResponder()
    }
     
    //返回
    @IBAction func returnBackButton(sender: UIBarButtonItem) {
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
